package com.example.liunanxin.lnx226ordering.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.CollectBean;
import com.example.liunanxin.lnx226ordering.url.Url;
import com.squareup.picasso.Picasso;

import java.util.List;


public class CollectionShopAdapter extends RecyclerView.Adapter<CollectionShopAdapter.ItemViewHolder> {

    private List<CollectBean> beans;
    CollectionShopAdapter.OnItemClickListener onItemClickListener;



    public interface OnItemClickListener {
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(CollectionShopAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    //刷新列表数据
    public void setData(List<CollectBean> collectBean){
        this.beans=collectBean;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
        return new CollectionShopAdapter.ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
        CollectBean collectBean=beans.get(i);
        final int position=i;
        Picasso.get()
                .load(Url.USERURL+collectBean.getPic())
                .into(itemViewHolder.img_shop);
        itemViewHolder.tv_shopname.setText(collectBean.getShopname());
        itemViewHolder.tv_shopaddress.setText(collectBean.getAddress());
        if(onItemClickListener!=null){
            itemViewHolder.itemview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.setItem(view,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }




    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView img_shop;
        TextView tv_shopname,tv_shopaddress;
        View itemview;
        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            img_shop=itemView.findViewById(R.id.imageView);
            tv_shopname=itemView.findViewById(R.id.textView1);
            tv_shopaddress=itemView.findViewById(R.id.textView2);
            itemview=itemView;
        }
    }
}

